<?php
/*
	[BuyPg] (C) 2010-2011 Buypg
	$Id: YouhuiAction.class.php 2012-02-23 09:42:25 wufujun
*/

if (!defined('THINK_PATH')){
	exit('Access Denied');
}

class YouhuiAction extends BpAction{
	//评论入库前操作
	public function _before_insertcomment(){
		global $_SGLOBAL;
		//判断用户针对该条信息是否已经发过评论
		$YouHuiCommentModel = M('YouhuiComment');
		$YouHuiComment = $YouHuiCommentModel->where("authorid = '{$_SGLOBAL['G_uid']}' and yid = '{$_POST['yid']}'")->find();
		if(!empty($YouHuiComment)){
			$this->error('您已经评论过这条优惠信息！');
		}
		
		//判断字数
		$lenTitle = mb_strlen($_POST['title'],'utf-8');
		$lenMerit = mb_strlen($_POST['merit'],'utf-8');
		$lenDefect = mb_strlen($_POST['defect'],'utf-8');
		$lenMessage = mb_strlen($_POST['message'],'utf-8');
		if($lenTitle > 100){
			$this->error('评论标题字数超过100');
		}
		if($lenMerit > 100){
			$this->error('评论优点字数超过100');
		}
		if($lenDefect > 100){
			$this->error('评论缺点字数超过100');
		}
		if($lenMessage > 200){
			$this->error('评论详细内容字数超过200');
		}
	}
	
	//优惠频道首页
	public function index(){
		global $_SGLOBAL;
		//优惠分类
		$YouHuiClass = getYouHuiClass();
		
		$YouHuiModel = M('Youhui');
		$YouHuiClassList = $YouHuiClass;
		foreach($YouHuiClassList as $key => $value){
			$YouHuiForClass = $YouHuiModel->where("cid = '{$value['id']}' and status = 1")->limit(5)->order('dateline desc')->select();
			foreach($YouHuiForClass as $k => $v){
				$YouHuiForClass[$k]['image_b'] = __ROOT__."/attachment/".$v['img'];
				$YouHuiForClass[$k]['image_m'] = __ROOT__."/attachment/".$v['img'].".m.jpg";
				$YouHuiForClass[$k]['image_s'] = __ROOT__."/attachment/".$v['img'].".s.jpg";
			}
			$YouHuiClassList[$key]['YouHuiInfo'] = $YouHuiForClass;
		}
		//当前位置
		$position = " &gt; <a href='".U('BuyPg://Youhui/')."'>优息信息</a>";
		//模板输出
		$this->assign('position' , $position);
		$this->assign('YouHuiClass' , $YouHuiClass);
		$this->assign('YouHuiClassList' , $YouHuiClassList);
		$this->display('buypg@Youhui:index');
	}
	
	//优惠分类页
	public function goods(){
		global $_SGLOBAL , $SeoSet;
		//优惠分类
		$YouHuiClass = getYouHuiClass();
		
		$cid = empty($_GET['cid']) ? 0 : intval($_GET['cid']);
		$YouHuiModel = M('Youhui');
		import("@.ORG.Page"); //导入分页类
		$count = $YouHuiModel->where("cid = '{$cid}' and status = 1")->count();    //计算总数
		$p = new Page ( $count, 20);
		$YouHuiList = $YouHuiModel->where("cid = '{$cid}' and status = 1")->limit($p->firstRow.','.$p->listRows)->order("dateline DESC")->select();
		if (empty($YouHuiList)){
			$this->redirect('/');
		}
		foreach($YouHuiList as $key => $value){
			$YouHuiList[$key]['image_b'] = __ROOT__."/attachment/".$value['img'];
			$YouHuiList[$key]['image_m'] = __ROOT__."/attachment/".$value['img'].".m.jpg";
			$YouHuiList[$key]['image_s'] = __ROOT__."/attachment/".$value['img'].".s.jpg";
		}
		$p->setConfig('header','篇记录');
		$p->setConfig('prev','<');
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');

		$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
		$page = $p->show(1);
		
		//当前位置
		$position = " &gt; <a href='".U('BuyPg://Youhui/')."'>优息信息</a> &gt; <a href='".U('BuyPg://Youhui/goods',array('cid' => $cid))."'>".getYouHuiClassNameByCid($cid)."</a>";
		
		//SEO设置
		$thisYouHuiClass = M('YouhuiClass')->where("id = $cid")->find();
		$pSeoSet = $SeoSet; //重新赋值父级SEO设置
		$SeoSet = array(
			'seotitle' => empty($thisYouHuiClass['seotitle']) ? $pSeoSet['seotitle'] : $thisYouHuiClass['seotitle'],
			'seokeywords' => empty($thisYouHuiClass['seokeywords']) ? $pSeoSet['seokeywords'] : $thisYouHuiClass['seokeywords'],
			'seodescription' => empty($thisYouHuiClass['seodescription']) ? $pSeoSet['seodescription'] : $thisYouHuiClass['seodescription']
		);
		//模板输出
		$this->assign('position' , $position);
		$this->assign('YouHuiClass' , $YouHuiClass);
		$this->assign('SeoSet', $SeoSet);
		$this->assign('page' , $page);
		$this->assign('YouHuiList' , $YouHuiList);
		$this->display('buypg@Youhui:goods');
	}

	//优惠详细页
	public function item(){
		global $_SGLOBAL , $SeoSet;
		$itemid = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$YouHuiModel = M("Youhui");
		$YouHuiItem = $YouHuiModel->where("id = $itemid and status = 1")->find();
		if(empty($YouHuiItem)){
			$this->redirect('/');
		}
		//产品展示图片路径处理
		$YouHuiItem['image_b'] = __ROOT__."/attachment/".$YouHuiItem['img'];
		$YouHuiItem['image_m'] = __ROOT__."/attachment/".$YouHuiItem['img'].".m.jpg";
		$YouHuiItem['image_s'] = __ROOT__."/attachment/".$YouHuiItem['img'].".s.jpg";

		//产品所属网站信息
		$WebSiteModel = D('NbSite');
		$WebSiteInfo = $WebSiteModel->getSite("site_id = '{$YouHuiItem['siteid']}' and flag = 1");
		if(empty($WebSiteInfo)){
			$this->redirect('/');
		}
		//处理经营范围
		$arrWebSiteDesc = explode(" / ",$WebSiteInfo['site_desc']);
		$WebSiteInfo['site_desc'] = '';
		foreach($arrWebSiteDesc as $key => $value){
			$WebSiteInfo['site_desc'] .= "<font>".$value."</font> ";
		}
		//处理综合得分
		$arrF7 = explode('.',$WebSiteInfo['f7']);

		//统计点击
		$YouHuiModel->setInc('apv',"id=$itemid",1);

		//评论
		$YouHuiCommentModel = M("YouhuiComment");
		$YouHuiCommentList = $YouHuiCommentModel->where("yid = $itemid and status = 1")->select();
		
		//当前位置
		$position = " &gt; <a href='".U('BuyPg://Youhui/')."'>优息信息</a> &gt; <a href='".U('BuyPg://Youhui/goods',array('cid' => $YouHuiItem['cid']))."'>".getYouHuiClassNameByCid($YouHuiItem['cid'])."</a> &gt; ".$YouHuiItem['title'];

		//SEO设置
		$SeoSet = array(
			'seotitle' => $YouHuiItem['title'],
			'seokeywords' => $YouHuiItem['seokeywords'],
			'seodescription' => $YouHuiItem['seodescription']
		);

		//模板输出
		$this->assign('YouHuiItem' , $YouHuiItem);
		$this->assign('SeoSet' , $SeoSet);
		$this->assign('position' , $position);
		$this->assign('WebSiteInfo',$WebSiteInfo);
		$this->assign('arrF7' , $arrF7);
		$this->assign('YouHuiCommentList', $YouHuiCommentList);
		$this->display('buypg@Youhui:item');
	}

	//添加评论
	public function insertcomment(){
		global $_SGLOBAL;
		$model = D ('YouhuiComment');
		$yid = empty($_POST['yid']) ? 0 : $_POST['yid'];
		//跳转页面
		$jumpUrl =  U(APP_NAME."://Youhui/item" , array('id' => $yid));
		$this->assign('jumpUrl' , $jumpUrl);
		
		//判断用户
		if(($mserr = checkstatus('user'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], $jumpUrl);
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}
		//作者信息赋值
		$_POST['authorid'] = $_SGLOBAL['G_uid'];
		$_POST['author'] = $_SGLOBAL['G_username'];

		//验证码
		if($_SESSION['verify'] != md5($_POST['verify'])) {
			$this->error('验证码错误！');
		}

		//内容过滤
		import("@.ORG.Input");
		$_POST['title'] = Input::nl2Br(Input::deleteHtmlTags($_POST['title']));
		$_POST['merit'] = Input::nl2Br(Input::deleteHtmlTags($_POST['merit']));
		$_POST['defect'] = Input::nl2Br(Input::deleteHtmlTags($_POST['defect']));
		$_POST['message'] = Input::nl2Br(Input::deleteHtmlTags($_POST['message']));
		
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}

		//入库操作
		$list = $model->add ();
		if ($list !== false) {
			$this->success('添加成功！');
		}else{
			$this->error('新增失败！');
		}

	}
}
?>
